#include "Reader.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Reader::Reader()
{
	processed	= false;
	fileLoaded	= false;
	clearReader();
}

Reader::Reader(char* path)
{
	processed	= false;
	fileLoaded	= false;
	clearReader();

	setFilePath(path);
}


Reader::~Reader()
{
	clearReader();
}


void Reader::setFilePath(char* path)
{
	if(fileLoaded)
		inputStream.close();

	strcpy(filePath, path);

	inputStream.open(filePath, ios::in | ios::binary); 

	_splitpath(filePath, fileDrive, fileDir, fileName, fileExt);

	if(inputStream.is_open())
		fileLoaded = true;
}


char Reader::getByte()
{
	char	data;

	if(!fileLoaded)
		return NULL;

	data = inputStream.get();

	return data;
}


short Reader::getWord()
{
	short	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));

	return data;
}


long Reader::getDword()
{
	long	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));

	return data;
}

float Reader::getFloat()
{
	float	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));

	return data;
}

char Reader::getByteNoMove()						
{
	char	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));
	inputStream.seekg(-(long)sizeof(data), ios::cur);

	return data;
}

short Reader::getWordNoMove()
{
	short	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));
	inputStream.seekg(-(long)sizeof(data), ios::cur);

	return data;
}

long Reader::getDwordNoMove()
{
	long	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));
	inputStream.seekg(-(long)sizeof(data), ios::cur);

	return data;
}

float Reader::getFloatNoMove()
{
	float	data;

	if(!fileLoaded)
		return NULL;

	inputStream.read((char*)(&data), sizeof(data));
	inputStream.seekg(-(long)sizeof(data), ios::cur);

	return data;
}


long Reader::getString(char* str, int len)
{
	if(!fileLoaded)
		return NULL;

	inputStream.getline(str, len, '\0'); 
	
	return inputStream.gcount();
}

long Reader::getStringNoMove(char* str, int len)
{
	if(!fileLoaded)
		return NULL;

	inputStream.getline(str, len, '\0'); 
	inputStream.seekg(-inputStream.gcount(), ios::cur);
	
	str[inputStream.gcount()] = 0;

	return inputStream.gcount();
}


long Reader::getLine( char* dest, int len )
{
	if(!fileLoaded)
		return NULL;

	inputStream.getline( dest, len ); 
	
	return inputStream.gcount();
}


void Reader::setPtrPos(long pos)
{
	if(!fileLoaded)
		return;

	inputStream.seekg(pos, ios::beg);
}

void Reader::movePtrPos(long pos)
{
	if(!fileLoaded) return;

	inputStream.seekg(pos, ios::cur);
}


void Reader::clearReader()
{
	fileLoaded = false;
	memset(filePath, 0, _MAX_PATH);
	memset(fileDrive, 0, _MAX_DRIVE);
	memset(fileDir, 0, _MAX_DIR);
	memset(fileName, 0, _MAX_FNAME);
	memset(fileExt, 0, _MAX_EXT);
}


long Reader::getSize()
{
	long	size;

	inputStream.seekg(0, ios::end);
	size = inputStream.tellg();
	inputStream.seekg(0, ios::beg);

	return size;
}


void Reader::getChunk(void* dest, long size)
{
	inputStream.read((char*)dest, size);
}

void Reader::getChunkNoMove(void* dest, long size)
{
	inputStream.read((char*)dest, size);
	inputStream.seekg(inputStream.gcount(), ios::cur);
}
